package kov.core.body.sb
{
	import com.riaoo.events.AnimationEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	
	import kov.core.BodyState;
	import kov.core.IBody;
	import kov.core.StateCommand;
	import kov.core.body.StateEnum;
	import kov.core.global.Global;
	
	public class SBBeiZhan extends BodyState
	{
		[Embed("../../../../../assets/sb/beizhan/0.gif")]
		private var Frame0:Class;
		
		public function SBBeiZhan(body:IBody, priority:int=1)
		{
			super(StateEnum.BEI_ZHAN, body, priority);
			createRenderer();
		}
		
		// 创建渲染帧。
		private function createRenderer():void
		{
			var f0:BitmapData = (new Frame0() as Bitmap).bitmapData;
			this.renderer.push(f0);
			this.renderer.push(f0);
		}
		
		override public function execute():void
		{
			this.body.renderer.frameSequence = this.renderer;
			this.body.renderer.delay = 90;
			this.body.renderer.offsetX = 52;
			this.body.renderer.offsetY = 73;
			this.body.renderer.gotoAndPlay(0);
			
			this.body.renderer.addEventListener(AnimationEvent.ANIMATION_ENTER_FRAME, onEnterFrame);
		}
		
		override public function exit():void
		{
			this.body.renderer.removeEventListener(AnimationEvent.ANIMATION_ENTER_FRAME, onEnterFrame);
		}
		
		private function onEnterFrame(event:AnimationEvent):void
		{
			if (Math.abs(Global.world.player.x - this.body.x) > 100 || Math.abs(Global.world.player.y - this.body.y) > 30)
			{
				if (Math.random() > 0.8)
				{
					this.body.executeStateCommand(new StateCommand(StateEnum.XING_ZOU));
				}
			}
			else // 打
			{
				if (Math.random() > 0.9)
				{
					this.body.executeStateCommand(new StateCommand(StateEnum.DA));
				}
			}
		}
		
	}
}